<?php
class GeraInterp
{
	private $dir;
	private $arq;
	private $opcao;
    private $msg;

	public function __construct($msg)
	{
		$this->dir = "/home/projetos/anaweb/anaweb";
		//$this->arq = $this->dir."/user/".$_SESSION['id_usuario']."/biblioteca.xml";
        $this->arq=$_SESSION['ANAWEB']->RetornaArquivoBiblioteca();
		$this->opcao = @$_GET['modo'];
        $this->msg = $msg;
	}
	public function opcao()
	{
		return $this->opcao;
	}
	public function menuopcoes()
	{
?>
		<form name="opcoes" action="index.php?inc=gera_interpolacao.php" method="GET">
			<p><input type="radio" name="modo" value="criar" id="criar"><label for="criar"><?php echo $this->msg->get('XML_GERA_MSG02') ?></label></p>
			<p><input type="radio" name="modo" value="alterar" id="alterar"><label for="alterar"><?php echo $this->msg->get('XML_GERA_MSG12') ?></label></p>
			<p><input type="radio" name="modo" value="remover" id="remover"><label for="remover"><?php echo $this->msg->get('XML_GERA_MSG13') ?></label></p>
            <p><input type="hidden" name="inc" value="gera_interpolacao.php">
			<p><input type="submit" class="button" name="proximo" value=<?php echo $this->msg->get('XML_NEXT') ?>></p>
		</form>
<?php
	}

	public function criarproblema()
	{
		//Botao 'Proximo >' ainda nao foi apertado, quer dizer que devo exibir o formulario de criacao
		if (!isset($_POST['proximo']) && !isset($_POST['criar']))
		{
?>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG01') ?></p>
		<form enctype="multipart/form-data" action="index.php?inc=gera_interpolacao.php&modo=criar" method="POST">
		<p><input type="radio" name="dados" value="discrete" id="discreto"><label for="discreto"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG02') ?></label></p>
		<p><input type="radio" name="dados" value="continuous" id="continuo"><label for="continuo"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG03') ?></p>
        <p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK') ?> onclick="history.back(1)"> <input class="button" type="submit" name="proximo" value=<?php echo $this->msg->get('XML_NEXT') ?>></p>
		</form>
<?php
		}
		//A opcao de dados discreto foi selecionada
		elseif ($_POST['dados'] == "discrete")
		{
			if (!isset($_POST['n']) || !is_numeric($_POST['n']) || $_POST['n'] > 10 || $_POST['n'] < 1)
			{
				if (isset($_POST['n']))
					echo "\t\t<p><font color=\"red\">O numero de pontos deve estar entre 1 e 10</font></p>\n";
?>
		<form method="POST" enctype="multipart/form-data" action="index.php?inc=gera_interpolacao.php&modo=criar">
		<p><label for="npto"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG04') ?></label> <input type="text" size='5' maxlength='5' name="n" id="npto"></p>
		<input type="hidden" name="dados" value="discrete">
		<p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK') ?> onclick="history.back(1)"> <input class="button" type="submit" name="proximo" value=<?php echo $this->msg->get('XML_NEXT') ?>></p>
		</form>
<?php
			}
			elseif (!isset($_POST['criar']))
			{
?>
		<form method="POST" action="index.php?inc=gera_interpolacao.php&modo=criar">
        <p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG04') ?> <? echo $_POST['n'] ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG017') ?></p>
		<table>
			<thead>
			<tr>
			   <th align="center"><p>x</p></th>
			   <th align="center"><p>,</p></th>
			   <th align="center"><p>y</p></th>
			</tr>
			</thead>
			<tbody>
			<? for ($i=0; $i<$_POST['n']; $i++) { ?>
			<tr>
				<td align="center">( <input type="text" size='5' maxlength='30' name="X[<? echo $i ?>]"></td>
				<td align="center"><p>,</p></td>
				<td align="center"><input type="text" size='5' maxlength='30' name="Y[<? echo $i ?>]"> )</td>
			</tr>
			<? } ?>
			</tbody>
		</table>
		<br>
		<p><label for="valor"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG018') ?></label> <input type="text" size='5' maxlength='8' name="valor" id="valor"></p>
		<p><label for="desc"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG019') ?>:</label></p>
		<textarea name="desc" cols="50" rows="3" id="desc"></textarea>
		<p><label for="com"><?php echo $this->msg->get('XML_GERA_MSG08') ?></label></p>
		<textarea name="com" cols="50" rows="3" id="com"></textarea>
		<p><label for="ref"><?php echo $this->msg->get('XML_GERA_MSG09') ?></label></p>
		<textarea name="ref" cols="50" rows="3" id="ref"></textarea>
		<input type="hidden" name="dados" value="discrete">
		<input type="hidden" name="n" value="<?php echo $_POST['n'] ?>">
		<p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK') ?> onclick="history.back(1)"> <input class="button" type="submit" name="criar" value="<?php echo $this->msg->get('Finalizar') ?>"></p>
<?php
			}
			else
			{
				function xmlentities($xml)
				{
					$entities = array ('<' => '&lt;','&' => '&amp;',);
					if (is_string($xml)) { $xml = strtr($xml, $entities);};
					return $xml;
				}
				function numerico($var)
				{
					if (!is_array($var)) return is_numeric($var);
					else
					{
						foreach ($var as $v)
							if (!numerico($v)) return false;
						return true;
					}
				}
				if (!numerico($_POST['X']) || !numerico($_POST['Y']))
				{
					die ("<p><font color=\"red\">Todas as coordenadas devem ser preenchidas com valores v&aacute;lidos.</font></p>");
				}
				if (empty($_POST['valor']))
				{
					die ("<p><font color=\"red\">O campo do valor a ser interpolado &eacute; obrigat&oacute;rio.</font></p>");
				}
				if (empty($_POST['desc']))
				{
					die ("<p><font color=\"red\">O campo de descri&ccedil;&atilde;o &eacute; obrigat&oacute;rio.</font></p>");
				}
				if (file_exists($this->arq)) {
					$objeto = new DOMDocument();
					$objeto->preserveWhiteSpace=false;
					$objeto->load($this->arq);
				}
				else {
					$objeto = new DOMDocument('1.0');
				}
				$objeto->formatOutput=true;
				if ($objeto->getelementsbytagname("problems")->length == 0)
				{
					$raiz = $objeto->createElement("problems");
					$raiz = $objeto->appendChild($raiz);
				}
				$raiz = $objeto->getelementsbytagname("problems")->item(0);
				if ($objeto->getelementsbytagname("interpolation")->length == 0) {
					$filho = $objeto->createElement("interpolation");
					$filho = $raiz->appendChild($filho);
				}
				$filho = $objeto->getelementsbytagname("interpolation")->item(0);
				$interpol = $objeto->createElement("interpol");
				$interpol = $filho->appendChild($interpol);
				$description = $objeto->createElement("description",xmlentities($_POST['desc']));
				$description = $interpol->appendChild($description);
				$options = $objeto->createElement("options");
				$options->setAttribute("data",$_POST['dados']);
				$options = $interpol->appendChild($options);
				$pontos = $objeto->createElement("num_points",$_POST['n']);
				$pontos = $interpol->appendChild($pontos);
				$interp_val = $objeto->createElement("interpol_value",$_POST['valor']);
				$interp_val = $interpol->appendChild($interp_val);
				for($i = 0; $i < $_POST['n']; $i++)
				{ 	$point = $objeto->createElement("point");
					$point = $interpol->appendChild($point);
					$x = $objeto->createElement("x",$_POST['X'][$i]);
					$x = $point->appendChild($x);
					$y = $objeto->createElement("y",$_POST['Y'][$i]);
					$y = $point->appendChild($y);
				}
				if (!empty($_POST['com'])) {
					$comen = $objeto->createElement("comments",xmlentities($_POST['com']));
					$comen = $interpol->appendChild($comen);
				}
				if (!empty($_POST['ref'])) {
					$refer = $objeto->createElement("reference",xmlentities($_POST['ref']));
					$refer = $interpol->appendChild($refer);
				}
				$objeto->save($this->arq);
?>
		<p>O exemplo foi criado com sucesso!</p>
		<p>Clique <a href="index.php?inc=gera_interpolacao.php">aqui</a> para voltar ao in&iacute;cio</p>
<?php
			}
		}
		//A opcao de dados continuo foi selecionada
		elseif ($_POST['dados'] == "continuous")
		{
			if (!isset($_POST['criar']))
			{
?>
		<form method="POST" enctype="multipart/form-data" action="index.php?inc=gera_interpolacao.php&modo=criar">
		<p><label for="npto"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG09') ?></label> <input type="text" size='5' maxlength='5' name="n" id="npto"></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG08') ?></p>
		<p><label for="func">f(x) = </label><input type="text" size='60' maxlength='100' name="funcao" id="func">
		<a href=javascript:winopen('ajuda/funcoesphp.php?idioma=PT_BR.php')><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG06') ?></a></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG07') ?> [<input type="text" size='5' maxlength='5' name="a">,<input type="text" size='5' maxlength='5' name="b">]</p>
		<p><input type="radio" name="distribuicao" value="uniform" id="uni"><label for="uni"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG012') ?></label></p>
		<p><input type="radio" name="distribuicao" value="chebyshev" id="cheby"><label for="cheby"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG013') ?></label></p>
		<p><label for="valor"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG018') ?></label> <input id="valor" type="text" size='5' maxlength='8' name="valor"></p>
		<p><label for="desc"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG019') ?></label></p>
		<textarea id="desc" name="desc" cols="50" rows="3"></textarea>
		<p><label for="com"><?php echo $this->msg->get('XML_GERA_MSG08') ?></label></p>
		<textarea id="com" name="com" cols="50" rows="3"></textarea>
		<p><label for="ref"><?php echo $this->msg->get('XML_GERA_MSG09') ?></label></p>
		<textarea id="ref" name="ref" cols="50" rows="3"></textarea>
		<input type="hidden" name="dados" value="continuous">
		<p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK') ?> onclick="history.back(1)"> <input class="button" type="submit" name="criar" value="<?php echo $this->msg->get('Finalizar') ?>"></p>
		</form>
<?php
			}
			else
			{
				function xmlentities($xml)
				{
					$entities = array ('<' => '&lt;','&' => '&amp;',);
					if (is_string($xml)) { $xml = strtr($xml, $entities);};
					return $xml;
				}
				if (empty($_POST['funcao']))
				{
					die("<p><font color=\"red\">O preenchimento da fun&ccedil;&atilde;o &eacute; obrigat&oacute;rio.</font></p>");
				}
				if (empty($_POST['desc']))
				{
					die("<p><font color=\"red\">O preenchimento da descri&ccedil;&atilde;o &eacute; obrigat&oacute;rio.</font></p>");
				}
				if (empty($_POST['n']))
				{
					die("<p><font color=\"red\">O preenchimento do n&uacute;mero de pontos a ser interpolado &eacute; obrigat&oacute;rio.</font></p>");
				}
				if (empty($_POST['valor']))
				{
					die("<p><font color=\"red\">O preenchimento do valor a ser interpolado &eacute; obrigat&oacute;rio.</font></p>");
				}
				if (file_exists($this->arq)) {
					$objeto = new DOMDocument();
					$objeto->preserveWhiteSpace=false;
					$objeto->load($this->arq);
				}
				else {
					$objeto = new DOMDocument('1.0');
				}
				$objeto->formatOutput=true;
				if ($objeto->getelementsbytagname("problems")->length == 0)
				{
					$raiz = $objeto->createElement("problems");
					$raiz = $objeto->appendChild($raiz);
				}
				$raiz = $objeto->getelementsbytagname("problems")->item(0);
				if ($objeto->getelementsbytagname("interpolation")->length == 0) {
					$filho = $objeto->createElement("interpolation");
					$filho = $raiz->appendChild($filho);
				}
				$filho = $objeto->getelementsbytagname("interpolation")->item(0);
				$interpol = $objeto->createElement("interpol");
				$interpol = $filho->appendChild($interpol);
				$description = $objeto->createElement("description",xmlentities($_POST['desc']));
				$description = $interpol->appendChild($description);
				$options = $objeto->createElement("options");
				$options->setAttribute("data",$_POST['dados']);
				$options = $interpol->appendChild($options);
		
				if (!empty($_POST['distribuicao'])) {
					$options = $objeto->createElement("options");
					$options->setAttribute("distribution",$_POST['distribuicao']);
					$options = $interpol->appendChild($options);
				}

				$pontos = $objeto->createElement("num_points",$_POST['n']);
				$pontos = $interpol->appendChild($pontos);
				$interp_val = $objeto->createElement("interpol_value",$_POST['valor']);
				$interp_val = $interpol->appendChild($interp_val);
		
				$function = $objeto->createElement("function",xmlentities($_POST['funcao']));
				$function = $interpol->appendChild($function);

				if (isset($_POST['a']) && isset($_POST['b'])) {
					$interval = $objeto->createElement("interval");
					$interval = $interpol->appendChild($interval);
					$x = $objeto->createElement("x",$_POST['a']);
					$x = $interval->appendChild($x);
					$y = $objeto->createElement("y",$_POST['b']);
					$y = $interval->appendChild($y);
				}
				if (!empty($_POST['com'])) {
					$comen = $objeto->createElement("comments",xmlentities($_POST['com']));
					$comen = $interpol->appendChild($comen);
				}
				if (!empty($_POST['ref'])) {
					$refer = $objeto->createElement("reference",xmlentities($_POST['ref']));
					$refer = $interpol->appendChild($refer);
				}
				$objeto->save($this->arq);
?>
		<p>O exemplo foi criado com sucesso!</p>
		<p>Clique <a href="index.php?inc=gera_interpolacao.php">aqui</a> para voltar ao in&iacute;cio</p>
<?php
			}
		}
	}
	public function	alterarproblema()
	{
		if (!isset($_POST['proximo']) && !isset($_POST['alterar']))
		{
			$objeto = new DomDocument();
			$objeto->load($this->arq);
			$filho = $objeto->getelementsbytagname("interpolation");
			if ($filho->length > 0)
			{
?>
	<p><?php echo $this->msg->get('Selecione_Exemplo_Alterado') ?></p>
	<form action="index.php?inc=gera_interpolacao.php&modo=alterar" method="POST">
<?php
				$interpol = $filho->item(0)->getelementsbytagname("interpol");
				for ($i=0; $i<$interpol->length; $i++)
				{
					$tipo = $interpol->item($i)->getelementsbytagname("options");
					foreach ($tipo as $t)
						if ($t->hasAttribute("data")) $tp = $t->getAttribute("data");
					if ($tp == "discrete") $tp = "discretos";
					else $tp = "cont&iacute;nuos";
?>
	<p><input type="radio" name="exemplo" value="<?php echo $i ?>" id="ex<?php echo $i ?>"> <label for="ex<?php echo $i ?>"> <?php echo $this->msg->get('Exemplo'); ?> <?php echo $i+1 ?> - <?php echo ($tp == "discretos")?$this->msg->get('XML_GERA_INTERPOLACAO_MSG02'):$this->msg->get('XML_GERA_INTERPOLACAO_MSG03'); ?></label></p>
<?php
				}
			}
?>
	<p><input type="button" value=<?php echo $this->msg->get('XML_BACK') ?> name="voltar" class="button" onclick="history.back(1)"> <input class="button" name="proximo" type="submit" value=<?php echo $this->msg->get('XML_NEXT') ?>></p>
	</form>
<?php
		}
		elseif (!isset($_POST['alterar']))
		{
			
			$objeto = new DomDocument();
			$objeto->load($this->arq);
			$filho = $objeto->getelementsbytagname("interpolation");
			if ($filho->length > 0)
			{
				$interpol = $filho->item(0)->getelementsbytagname("interpol");
				$ex = $interpol->item($_POST['exemplo']);
				$option = $ex->getelementsbytagname("options");
				foreach ($option as $opt)
				{
					if ($opt->hasAttribute("data")) $tp = $opt->getAttribute("data");
					elseif ($opt->hasAttribute("distribution")) $dst = $opt->getAttribute("distribution");
				}
				$desc = $ex->getelementsbytagname("description");
				$desc = $desc->item(0)->nodeValue;
				$intv = $ex->getelementsbytagname("interpol_value");
				$intv = $intv->item(0)->nodeValue;
				$npto = $ex->getelementsbytagname("num_points");
				$npto = $npto->item(0)->nodeValue;

				$com = $ex->getelementsbytagname("comments");
				if ($com->length) $com = $com->item(0)->nodeValue;
				else $com = "";

				$ref = $ex->getelementsbytagname("reference");
				if ($ref->length) $ref = $ref->item(0)->nodeValue;
				else $ref = "";

				if ($tp == "discrete")
				{
					$points = $ex->getelementsbytagname("point");
					for ($i = 0; $i < $points->length; $i++) {
						$pto[$i]['x'] = $points->item($i)->getelementsbytagname("x")->item(0)->nodeValue;
						$pto[$i]['y'] = $points->item($i)->getelementsbytagname("y")->item(0)->nodeValue;
					}
?>
		<form method="POST" action="index.php?inc=gera_interpolacao.php&modo=alterar">
        <p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG016') ?> <? echo $npto ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG017') ?></p>
		<table>
			<thead>
			<tr>
			   <th align="center"><p>x</p></th>
			   <th align="center"><p>,</p></th>
			   <th align="center"><p>y</p></th>
			</tr>
			</thead>
			<tbody>
			<? for ($i=0; $i<$npto; $i++) { ?>
			<tr>
				<td align="center">( <input type="text" size='5' maxlength='30' name="X[<? echo $i ?>]" value="<?php echo $pto[$i]['x'] ?>"></td>
				<td align="center"><p>,</p></td>
				<td align="center"><input type="text" size='5' maxlength='30' name="Y[<? echo $i ?>]" value="<?php echo $pto[$i]['y'] ?>"> )</td>
			</tr>
			<? } ?>
			</tbody>
		</table>
		<br>
		<p><label for="valor"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG018'); ?></label> <input type="text" size='5' maxlength='8' name="valor" id="valor" value="<?php echo $intv ?>"></p>
		<p><label for="desc"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG019'); ?></label></p>
		<textarea name="desc" cols="50" rows="3" id="desc"><?php if (!empty($desc)) echo $desc ?></textarea>
		<p><label for="com"><?php echo $this->msg->get('XML_GERA_MSG08'); ?></label></p>
		<textarea name="com" cols="50" rows="3" id="com"><?php if (!empty($com)) echo $com ?></textarea>
		<p><label for="ref"><?php echo $this->msg->get('XML_GERA_MSG09'); ?></label></p>
		<textarea name="ref" cols="50" rows="3" id="ref"><?php if (!empty($ref)) echo $ref ?></textarea>
		<input type="hidden" name="dados" value="discrete">
		<input type="hidden" name="n" value="<?php echo $npto ?>">
		<input type="hidden" name="exemplo" value="<?php echo $_POST['exemplo'] ?>">
		<p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK'); ?> onclick="history.back(1)"> <input class="button" type="submit" name="alterar" value="<?php echo $this->msg->get('Alterar'); ?>"></p>
<?php
				}
                //dados continuos
                else
                {
                    $func = $ex->getelementsbytagname("function");
                    $func = ($func->length)?$func->item(0)->nodeValue:"";
                    $interval = $ex->getelementsbytagname("interval");
                    if ($interval->length)
                    {
                        $inta = $interval->item(0)->getelementsbytagname("x");
                        $inta = ($inta->length)?$inta->item(0)->nodeValue:"";
                        $intb = $interval->item(0)->getelementsbytagname("y");
                        $intb = ($intb->length)?$intb->item(0)->nodeValue:"";
                    }
?>
        <form method="POST" enctype="multipart/form-data" action="index.php?inc=gera_interpolacao.php&modo=alterar">
        <p><label for="npto"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG04') ?></label> <input type="text" size='5' maxlength='5' name="n" id="npto" value="<?php echo $npto ?>"></p>
        <p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG05') ?></p>
        <p><label for="func">f(x) = </label><input type="text" size='60' maxlength='100' name="funcao" id="func" value="<?php echo $func ?>">
        <a href=javascript:winopen('ajuda/funcoesphp.php?idioma=PT_BR.php')><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG06') ?></a></p>
        <p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG07') ?> [<input type="text" size='5' maxlength='5' name="a" value="<?php echo $inta ?>">,<input type="text" size='5' maxlength='5' name="b" value="<?php echo $intb ?>">]</p>
        <p><input type="radio" name="distribuicao" value="uniform" id="uni"<?php if ($dst == "uniform") echo " checked" ?>><label for="uni"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG012') ?></label></p>
        <p><input type="radio" name="distribuicao" value="chebyshev" id="cheby"<?php if ($dst == "chebyshev") echo " checked" ?>><label for="cheby"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG013') ?></label></p>
        <p><label for="valor"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG018') ?></label> <input id="valor" type="text" size='5' maxlength='8' name="valor" value="<?php echo $intv ?>"></p>
        <p><label for="desc"><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG019') ?></label></p>
        <textarea name="desc" cols="50" rows="3" id="desc"><?php if (!empty($desc)) echo $desc ?></textarea>
        <p><label for="com"><?php echo $this->msg->get('XML_GERA_MSG08') ?></label></p>
        <textarea name="com" cols="50" rows="3" id="com"><?php if (!empty($com)) echo $com ?></textarea>
        <p><label for="ref"><?php echo $this->msg->get('XML_GERA_MSG09') ?></label></p>
        <textarea name="ref" cols="50" rows="3" id="ref"><?php if (!empty($ref)) echo $ref ?></textarea>
        <input type="hidden" name="dados" value="continuous">
        <input type="hidden" name="exemplo" value="<?php echo $_POST['exemplo'] ?>">
        <p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK') ?> onclick="history.back(1)"> <input class="button" type="submit" name="alterar" value="<?php echo $this->msg->get('Alterar') ?>"></p>
        </form>
<?php
                }
            }
        }
        else
        {
			function xmlentities($xml)
			{
				$entities = array ('<' => '&lt;','&' => '&amp;',);
				if (is_string($xml)) { $xml = strtr($xml, $entities);}
				return $xml;
			}
			if (is_file($this->arq))
			{
				$objeto = new DOMDocument();
				$objeto->preserveWhiteSpace=false;
				$objeto->load($this->arq);
				$objeto->formatOutput=true;
				$filho = $objeto->getelementsbytagname("interpolation");
				if ($filho->length > 0)
				{
					$temp = $filho->item(0)->getelementsbytagname("interpol")->item($_POST['exemplo']);

					$dsc = $temp->getelementsbytagname("description");
					$dsc->item(0)->nodeValue = xmlentities(utf8_encode($_POST['desc']));

					$intv = $temp->getelementsbytagname("interpol_value");
					$intv->item(0)->nodeValue = $_POST['valor'];

					$ref = $temp->getelementsbytagname("reference");
					if ($ref->length)
						$ref->item(0)->nodeValue = (empty($_POST['ref']))?"":xmlentities(utf8_encode($_POST['ref']));
					elseif (!empty($_POST['ref']))
					{
						$ref = $objeto->createElement("reference",xmlentities(utf8_encode($_POST['ref'])));
						$ref = $temp->appendChild($ref);
					}

					$com = $temp->getelementsbytagname("comments");
					if ($com->length)
						$com->item(0)->nodeValue = (empty($_POST['com']))?"":xmlentities(utf8_encode($_POST['com']));
					elseif (!empty($_POST['com']))
					{
						$com = $objeto->createElement("comments",xmlentities(utf8_encode($_POST['com'])));
						$com = $temp->appendChild($com);
					}
					if ($_POST['dados'] == "discrete")
					{
						$points = $temp->getelementsbytagname("point");
						for($i = 0; $i < $points->length; $i++)
						{
							$points->item($i)->getelementsbytagname("x")->item(0)->nodeValue = $_POST['X'][$i];
							$points->item($i)->getelementsbytagname("y")->item(0)->nodeValue = $_POST['Y'][$i];
						}
					}
					elseif ($_POST['dados'] == "continuous")
					{
						$nump = $temp->getelementsbytagname("num_points");
						$nump->item(0)->nodeValue = $_POST['n'];

						$func = $temp->getelementsbytagname("function");
						$func->item(0)->nodeValue = $_POST['funcao'];

						$interval = $temp->getelementsbytagname("interval");
                        if ($interval->length)
                        {
                            $interval = $interval->item(0);
                            $interval->getelementsbytagname("x")->item(0)->nodeValue = $_POST['a'];
                            $interval->getelementsbytagname("y")->item(0)->nodeValue = $_POST['b'];
                        }
                        elseif (isset($_POST['a']) && isset($_POST['b']))
                        {
                            $interval = $objeto->createElement("interval");
                            $interval = $temp->appendChild($interval);
                            $x = $objeto->createElement("x",$_POST['a']);
                            $x = $interval->appendChild($x);
                            $y = $objeto->createElement("y",$_POST['b']);
                            $y = $interval->appendChild($y);
                        }
						$option = $temp->getelementsbytagname("options");
						foreach ($option as $opt)
						{
							if ($opt->hasAttribute("distribution")) $opt->setAttribute("distribution", $_POST['distribuicao']);
						}
					}
					$objeto->save($this->arq);
?>
		<p>Problema alterado com sucesso!</p>
		<p><a href="index.php?inc=gera_interpolacao.php">In&iacute;cio</a></p>
<?php
				}
			}
		}
	}
	public function removerproblema()
	{
		if (!isset($_POST['proximo']) && !isset($_POST['remover']))
		{
			$objeto = new DomDocument();
			$objeto->load($this->arq);
			$filho = $objeto->getelementsbytagname("interpolation");
			if ($filho->length > 0)
			{
?>
	<p><?php echo $this->msg->get('Selecione_Exemplo_Removido') ?>:</p>
	<form action="index.php?inc=gera_interpolacao.php&modo=remover" method="POST">
<?php
				$interpol = $filho->item(0)->getelementsbytagname("interpol");
				for ($i=0; $i<$interpol->length; $i++)
				{
					$tipo = $interpol->item($i)->getelementsbytagname("options");
					foreach ($tipo as $t)
						if ($t->hasAttribute("data")) $tp = $t->getAttribute("data");
					if ($tp == "discrete") $tp = "discretos";
					else $tp = "cont&iacute;nuos";
?>
	<p><input type="radio" name="exemplo" value="<?php echo $i ?>" id="ex<?php echo $i ?>"> <label for="ex<?php echo $i ?>"> <?php echo $this->msg->get('Exemplo') ?> <?php echo $i+1 ?> - <?php echo ($tp == "discretos")?$this->msg->get('XML_GERA_INTERPOLACAO_MSG02'):$this->msg->get('XML_GERA_INTERPOLACAO_MSG03'); ?></label></p>
<?php
				}
			}
?>
	<p><input type="button" value=<?php echo $this->msg->get('XML_BACK') ?> name="voltar" class="button" onclick="history.back(1)"> <input class="button" name="proximo" type="submit" value=<?php echo $this->msg->get('XML_NEXT') ?>></p>
	</form>
<?php
		}
		elseif (!isset($_POST['remover']))
		{
?>
	<p><?php echo $this->msg->get('Seguinte_Exemplo_Sera_Removido') ?></p>
<?php
			$objeto = new DomDocument();
			$objeto->load($this->arq);
			$filho = $objeto->getelementsbytagname("interpolation");
			if ($filho->length > 0)
			{
				$interpol = $filho->item(0)->getelementsbytagname("interpol");
				$ex = $interpol->item($_POST['exemplo']);
				$option = $ex->getelementsbytagname("options");
				foreach ($option as $opt)
				{
					if ($opt->hasAttribute("data")) $tp = $opt->getAttribute("data");
					elseif ($opt->hasAttribute("distribution")) $dst = $opt->getAttribute("distribution");
				}
				$desc = $ex->getelementsbytagname("description");
				$desc = $desc->item(0)->nodeValue;
				$intv = $ex->getelementsbytagname("interpol_value");
				$intv = $intv->item(0)->nodeValue;
				$npto = $ex->getelementsbytagname("num_points");
				$npto = $npto->item(0)->nodeValue;

				$com = $ex->getelementsbytagname("comments");
				if ($com->length) $com = $com->item(0)->nodeValue;
				else $com = "";

				$ref = $ex->getelementsbytagname("reference");
				if ($ref->length) $ref = $ref->item(0)->nodeValue;
				else $ref = "";

				if ($tp == "discrete")
				{
					$points = $ex->getelementsbytagname("point");
					for ($i = 0; $i < $points->length; $i++) {
						$pto[$i]['x'] = $points->item($i)->getelementsbytagname("x")->item(0)->nodeValue;
						$pto[$i]['y'] = $points->item($i)->getelementsbytagname("y")->item(0)->nodeValue;
					}
?>
		<form method="POST" action="index.php?inc=gera_interpolacao.php&modo=remover">
        <p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG04') ?> <? echo $npto ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG017') ?></p>
		<table>
			<thead>
			<tr>
			   <th align="center"><p>x</p></th>
			   <th align="center"><p>,</p></th>
			   <th align="center"><p>y</p></th>
			</tr>
			</thead>
			<tbody>
			<? for ($i=0; $i<$npto; $i++) { ?>
			<tr>
				<td align="center">( <?php echo $pto[$i]['x'] ?></td>
				<td align="center">,</td>
				<td align="center"><?php echo $pto[$i]['y'] ?> )</td>
			</tr>
			<? } ?>
			</tbody>
		</table>
		<br>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG018') ?> <?php echo $intv ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG019') ?> <?php if (!empty($desc)) echo $desc ?></p>
		<p><?php echo $this->msg->get('XML_GERA_MSG08') ?> <?php if (!empty($com)) echo $com ?></p>
		<p><?php echo $this->msg->get('XML_GERA_MSG09') ?> <?php if (!empty($ref)) echo $ref ?></p>
		<input type="hidden" name="exemplo" value="<?php echo $_POST['exemplo'] ?>">
		<p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK') ?> onclick="history.back(1)"> <input class="button" type="submit" name="remover" value="<?php echo $this->msg->get('Remover') ?>"></p>
<?php
				}
				//dados continuos
				else
				{
                    $func = $ex->getelementsbytagname("function");
                    $func = ($func->length)?$func->item(0)->nodeValue:"";
                    $interval = $ex->getelementsbytagname("interval");
                    if ($interval->length)
                    {
                        $inta = $interval->item(0)->getelementsbytagname("x");
                        $inta = ($inta->length)?$inta->item(0)->nodeValue:"";
                        $intb = $interval->item(0)->getelementsbytagname("y");
                        $intb = ($intb->length)?$intb->item(0)->nodeValue:"";
                    }
?>
		<form method="POST" enctype="multipart/form-data" action="index.php?inc=gera_interpolacao.php&modo=remover">
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG04') ?> <?php echo $npto ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG05') ?> f(x) = <?php echo $func ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG07') ?> [<?php echo $inta ?>,<?php echo $intb ?>]</p>
		<p><?php echo (($dst == "chebyshev")?$this->msg->get('XML_GERA_INTERPOLACAO_MSG013'):$this->msg->get('XML_GERA_INTERPOLACAO_MSG012')) ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG018') ?> <?php echo $intv ?></p>
		<p><?php echo $this->msg->get('XML_GERA_INTERPOLACAO_MSG019') ?> <?php if (!empty($desc)) echo $desc ?></p>
		<p><?php echo $this->msg->get('XML_GERA_MSG08') ?> <?php if (!empty($com)) echo $com ?></p>
		<p><?php echo $this->msg->get('XML_GERA_MSG09') ?> <?php if (!empty($ref)) echo $ref ?></p>
		<input type="hidden" name="exemplo" value="<?php echo $_POST['exemplo'] ?>">
		<p><input class="button" type="button" name="voltar" value=<?php echo $this->msg->get('XML_BACK') ?> onclick="history.back(1)"> <input class="button" type="submit" name="remover" value="<?php echo $this->msg->get('Remover') ?>"></p>
		</form>
<?php
				}
			}
		}
		else
		{
			if (is_file($this->arq))
			{
				$objeto = new DOMDocument();
				$objeto->load($this->arq);
				$objeto->formatOutput=true;
				$pai = $objeto->getelementsbytagname("interpolation");
				if ($pai->length)
				{
					$filho = $pai->item(0)->getelementsbytagname("interpol");
					$aremover = $filho->item($_POST['exemplo']);
					$oldfilho = $pai->item(0)->removeChild($aremover);
					$objeto->save($this->arq);
?>
		<p>Exemplo removido com sucesso</p>
		<p><a href="index.php?inc=gera_interpolacao.php">In&iacute;cio</a></p>
<?php
				}
			}
		}
	}

	public function header()
	{
?>
    <script language="JavaScript">
    function winopen(pagina) {
    window.open(pagina,'janela','width=500,height=380,top=150,left=300,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,titlebar=yes');
    }
    </script>
	<h1><?php echo $this->msg->get('Autoria_Problemas') ?></h1>
	<h2><?php echo $this->msg->get('Interpolacao') ?></h2>
<?php
	}
	public function footer()
	{

	}
	
}

session_start();

$gera = new GeraInterp($msg);
$gera->header();
//$opcao = $gera->opcao();
switch ($gera->opcao())
{
	case "criar":
		$gera->criarproblema();
		break;
	case "alterar":
		$gera->alterarproblema();
		break;
	case "remover":
		$gera->removerproblema();
		break;
	default:
		$gera->menuopcoes();
}

$gera->footer();

?>